﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Data.SqlClient;
using DigitalRune.Windows.TextEditor;
using System.IO;

namespace Zove.Desktop.Tools.Database
{
    public partial class DatabaseQueryView : DockContent, IDesktopView
    {

        public SqlConnection Connection
        {
            get;
            set;
        }

        public DatabaseQueryView()
        {
            InitializeComponent();

            Load += new EventHandler(DatabaseQueryView_Load);
        }

        public DatabaseQueryView(SqlConnection connection)
        {
            this.Connection = connection;
        }

        public void DatabaseQueryView_Load(object sender, EventArgs e)
        {
        }

        #region IDesktopForm Members

        public void RunInBackground()
        {
        }

        public void Run()
        {
            if (Connection == null)
            {
                DatabaseLoginView view = new DatabaseLoginView();

                if (view.ShowDialog() == DialogResult.OK)
                {

                }
                else
                {
                    return;
                }
            }

            if (Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
            }

            DataTable table = new DataTable();
            SqlDataAdapter adapter
                = new SqlDataAdapter(this.tecSource.Text, Connection);
            adapter.Fill(table);

            this.gvDataTable.DataSource = table;
        }

        public TextEditorControl GetTextEditorControl()
        {
            return this.tecSource;
        }

        public string GetFileName()
        {
            throw new NotImplementedException();
        }

        public string GetFileExtension()
        {
            return ".sql";
        }

        public void Save(string fileName)
        {
            File.WriteAllText(fileName, this.tecSource.Text);
        }

        public string GetFileFilter()
        {
            throw new NotImplementedException();
        }

        public string GetColourScheme()
        {
            return "SQL";
        }

        #endregion

        #region IDesktopForm Members


        public new void Validate()
        {
            throw new NotImplementedException();
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDesktopForm Members


        public void Debug() {
            throw new NotImplementedException();
        }

        public void Resume() {
            throw new NotImplementedException();
        }

        public void Restart() {
            throw new NotImplementedException();
        }

        public double Profile() {
            throw new NotImplementedException();
        }

        #endregion
    }
}
